#include "component/Transform.h"
#include "GameObject.h"
#include "Timer.h"
#include "physics/Physics.h"




using namespace Boon;

GameObject::RegisterCreationCB<Transform> t;




void Boon::Transform::OnCreate()
{
	m_IsDirty = true;
	m_physics = nullptr;
}

void Boon::Transform::OnDestroy()
{

}

void Boon::Transform::OnStart()
{
	m_physics = Physics::GetPhysics();
}

void Boon::Transform::OnEnable()
{

}

void Boon::Transform::OnDisable()
{

}


void Boon::Transform::Update()
{
	if( m_IsDirty && m_physics )
	{
		m_physics->SetTransform( *this );
		m_IsDirty = false;
	}
}


void Boon::Transform::LateUpdate()
{

}

const Matrix44& Boon::Transform::GetMatrixToWorld() const
{
	return m_ToWorld;
}

const Matrix44& Boon::Transform::GetMatrixFromWorld() const
{
	return m_FromWorld;
}

const Vector3 Boon::Transform::GetPosition() const
{
	return m_ToWorld.GetPosition();
}

void Boon::Transform::Translate( const Vector3& direction )
{
	m_ToWorld.Translate( direction );
}

void Boon::Transform::SetPosition( const Vector3& position )
{
	m_IsDirty = true;
	m_ToWorld.SetPosition( position );
	if( m_physics )
		m_physics->SetTransform( *this );

}

void Boon::Transform::SetPosition( float x, float y, float z )
{
	SetPosition( Vector3( x, y, z ) );
}

const Quaternion& Boon::Transform::GetRotation() const
{
	return m_Rotation;
}

void Boon::Transform::SetRotation( const Quaternion& q )
{
	m_Rotation = q;
	m_ToWorld.Rotate( q );
}

void Boon::Transform::Rotate( float angle, const Vector3& axis /*= Vector3::FORWARD */ )
{
	Quaternion q( angle, axis );
	m_Rotation *= q;
	m_ToWorld.Rotate( q );
	m_IsDirty = true;
}
